package com.example.datadashboard.controller;

import com.example.datadashboard.common.Result;
import com.example.datadashboard.entity.Widget;
import com.example.datadashboard.service.WidgetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/widget")
public class WidgetController {
    @Autowired
    private WidgetService widgetService;

    @GetMapping("/{id}")
    public Result<Widget> getById(@PathVariable Long id) {
        return Result.success(widgetService.getById(id));
    }

    @GetMapping("/dashboard/{dashboardId}")
    public Result<List<Widget>> getDashboardWidgets(@PathVariable Long dashboardId) {
        return Result.success(widgetService.getDashboardWidgets(dashboardId));
    }

    @PostMapping("/position")
    public Result<Boolean> updatePosition(
            @RequestParam Long widgetId,
            @RequestParam Integer x,
            @RequestParam Integer y,
            @RequestParam Integer width,
            @RequestParam Integer height) {
        widgetService.updateWidgetPosition(widgetId, x, y, width, height);
        return Result.success(true);
    }

    @PostMapping
    public Result<Boolean> save(@RequestBody Widget widget) {
        return Result.success(widgetService.save(widget));
    }

    @PutMapping
    public Result<Boolean> update(@RequestBody Widget widget) {
        return Result.success(widgetService.updateById(widget));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(widgetService.removeById(id));
    }
}
